﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Resources;
using _06_坦克大战_正式.Properties;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

namespace _06_坦克大战_正式
{
    internal class GameObjectManager
    {
        private static List<NotMovething> wallList = new List<NotMovething>();
        private static List<NotMovething> steelList = new List<NotMovething>();
        private static List<EnemtTank> tankList = new List<EnemtTank>();
        private static List<Bullet> bulletList = new List<Bullet>();
        private static List<Explosion> expList = new List<Explosion>();
        private static NotMovething boss;
        private static MyTank myTank;
        private static int enemyBornSpeed = 120;
        private static int enemyBornCount = 60;
        private static Point[] points = new Point[3];
        private static object _bulletLock = new object();
        private static NotMovething Hp;


        public static void Start()
        {
            points[0].X = 0; points[0].Y = 0;
            points[1].X = 7 * 30; points[1].Y = 0;
            points[2].X = 14 * 30;points[2].Y = 0;
        }
        public static void Update()
        {
            foreach (NotMovething nm in wallList)
            {
                nm.Update();
            }
            foreach (NotMovething nm in steelList)
            {
                nm.Update();
            }
            foreach(EnemtTank tank in tankList)
            {
                tank.Update();
            }
            CheckAndDestroyBullet();
            CheckAndDestroyExplosion();
            foreach (Bullet bullet in bulletList)
            {
                bullet.Update();
            }
            foreach(Explosion exp in expList)
            {
                exp.Update();
            }
            
            boss.Update();
            Hp.Update();
            myTank.Update();
            EnemyBorn();
        }
        private static void CheckAndDestroyExplosion()
        {
            List<Explosion> needToDestroy = new List<Explosion>();
            foreach (Explosion exp in expList)
            {
                if (exp.IsNeedDestroy == true)
                {
                    needToDestroy.Add(exp);
                }
            }

            foreach (Explosion exp in needToDestroy)
            {
                expList.Remove(exp);
            }
        }
        
        private static void CheckAndDestroyBullet()
        {
            List<Bullet> needToDestroy = new List<Bullet>();
            foreach(Bullet bullet in bulletList)
            {
                if(bullet.IsDestroy == true)
                {
                    needToDestroy.Add(bullet);
                }
            }

            foreach(Bullet bullet in needToDestroy)
            {
                bulletList.Remove(bullet);
            }
        }
        private static void EnemyBorn()
        {
            enemyBornCount++;
            if(enemyBornCount < enemyBornSpeed) return;

            SoundManager.Playadd();
            //0-2
            Random rd = new Random();
            int index = rd.Next(0,3);

            Point position = points[index]; 

            int enemyType = rd.Next(1,5);
            switch (enemyType)
            {
                case 1:
                    CreateEnemyTank1(position.X,position.Y);
                    break;
                case 2:
                    CreateEnemyTank2(position.X, position.Y);
                    break;
                case 3:
                    CreateEnemyTank3(position.X, position.Y);
                    break;
                case 4:
                    CreateEnemyTank4(position.X, position.Y);
                    break;
                default:

                    break;
            }
            enemyBornCount = 0;
        }
        private static void CreateEnemyTank1(int x,int y)
        {
            EnemtTank tank = new EnemtTank(x, y, 2, Resources.GrayDown, Resources.GrayUp, Resources.GrayRight, Resources.GrayRight);
            tankList.Add(tank);
        }
        private static void CreateEnemyTank2(int x,int y) 
        {
            EnemtTank tank = new EnemtTank(x, y, 2, Resources.GreenDown, Resources.GreenUp, Resources.GreenRight, Resources.GreenRight);
            tankList.Add(tank);
        }
        private static void CreateEnemyTank3(int x,int y)
        {
            EnemtTank tank = new EnemtTank(x, y, 4, Resources.QuickDown, Resources.QuickUp, Resources.QuickRight, Resources.QuickRight);
            tankList.Add(tank);
        }
        private static void CreateEnemyTank4(int x,int y)
        {
            EnemtTank tank = new EnemtTank(x, y, 1, Resources.SlowDown, Resources.SlowUp, Resources.SlowRight, Resources.SlowRight);
            tankList.Add(tank);
        }
        public static NotMovething IsColliedeWall(Rectangle rect)
        {
            foreach(NotMovething wall in wallList)
            {
                if (wall.GetRectangle().IntersectsWith(rect))
                {
                    return wall;
                }
            }
            return null;
        }
        public static NotMovething IsColliedeSteel(Rectangle rect)
        {
            foreach (NotMovething steel in steelList)
            {
                if (steel.GetRectangle().IntersectsWith(rect))
                {
                    return steel;
                }
            }
            return null;
        }
        public static bool IsColliedeBoss(Rectangle rect)
        {
            if (boss.GetRectangle().IntersectsWith(rect))
            {
                return true;
            }
            return false;
        }
        public static EnemtTank IsCollidedEnemyTank(Rectangle rt)
        {
            foreach(EnemtTank tank in tankList)
            {
                if (tank.GetRectangle().IntersectsWith(rt))
                {
                    return tank;
                }
            }

            return null;
        }
        public static MyTank IsCollideMyTank(Rectangle rt)
        {
            if(myTank.GetRectangle().IntersectsWith(rt)) return myTank;

            return null;
        }
        public static void DestroyWall(NotMovething wall)
        {
            wallList.Remove(wall);
        }
        public static void DestroyTank(EnemtTank tank)
        {
            tankList.Remove(tank);
        }
        //public static void DrawMap()
        //{
        //    foreach(NotMovething nm in wallList)
        //    {
        //        nm.DrawSelf();
        //    }
        //    foreach(NotMovething nm in steelList)
        //    {
        //        nm.DrawSelf();
        //    }
        //    boss.DrawSelf();
        //}
        //public static void DrawMytank()
        //{
        //    myTank.DrawSelf();
        //}
        public static void CreateMytank()
        {
            int x = 5 * 30; 
            int y = 14 * 30;

            myTank = new MyTank(x, y, 2);
        }
        public static void CreateMap()
        {
            CreatWall(1, 1, 5,Resources.wall,wallList);
            CreatWall(3, 1, 5, Resources.wall, wallList);
            CreatWall(5, 1, 4, Resources.wall, wallList);
            CreatWall(7, 1, 3, Resources.wall, wallList);
            CreatWall(9, 1, 4, Resources.wall, wallList);
            CreatWall(11, 1, 5, Resources.wall, wallList);
            CreatWall(13, 1, 5, Resources.wall, wallList);

            CreatWall(7, 5, 1, Resources.steel, steelList);
            CreatWall(0, 7, 1, Resources.steel, steelList);
            CreatWall(14, 7, 1, Resources.steel, steelList);


            CreatWall(2, 7, 1, Resources.wall, wallList);
            CreatWall(3, 7, 1, Resources.wall, wallList);
            CreatWall(4, 7, 1, Resources.wall, wallList);

            CreatWall(6, 6, 1, Resources.wall, wallList);
            CreatWall(7, 6, 2, Resources.wall, wallList);
            CreatWall(8, 6, 1, Resources.wall, wallList);

            CreatWall(11, 7, 1, Resources.wall, wallList);
            CreatWall(10, 7, 1, Resources.wall, wallList);
            CreatWall(12, 7, 1, Resources.wall, wallList);

            CreatWall(1, 9, 5, Resources.wall, wallList);
            CreatWall(3, 9, 5, Resources.wall, wallList);
            CreatWall(5, 9, 2, Resources.wall, wallList);
            CreatWall(6, 10, 1, Resources.wall, wallList);
            CreatWall(7, 9, 3, Resources.wall, wallList);
            CreatWall(8, 10, 1, Resources.wall, wallList);
            CreatWall(9, 9, 2, Resources.wall, wallList);
            CreatWall(11, 9, 5, Resources.wall, wallList);
            CreatWall(13, 9, 5, Resources.wall, wallList);

            CreatWall(6, 13, 2, Resources.wall, wallList);
            CreatWall(7, 13, 1, Resources.wall, wallList);
            CreatWall(8, 13, 2, Resources.wall, wallList);

            CreateBoss(7, 14, Resources.Boss);
            CreateHP(8, 14, Resources.GEMAPPLE);

        }
        private static void CreateBoss(int x,int y,Image img)
        {
            int xPosition = x * 30;
            int yPosition = y * 30;
            boss = new NotMovething(xPosition,yPosition,img);

        }
        public static void CreateHP(int x,int y,Bitmap bmp)
        {
            int xPosition = x;
            int yPosition = y - bmp.Height;
            Hp = new NotMovething(xPosition, yPosition, bmp);
        }
        public static void CreatWall(int x,int y,int count,Image img,List<NotMovething> walllist)
        {
            int xPosition = x * 30;
            int yPosition = y * 30;
            for(int i = yPosition; i < yPosition + count*30; i+= 15)
            {
                NotMovething wall1 = new NotMovething(xPosition, i, img);
                NotMovething wall2 = new NotMovething(xPosition + 15, i, img);
                walllist.Add(wall1);
                walllist.Add(wall2); 
            }
        }
        public static void CreateExplosion(int x,int y)
        {
            
            Explosion exp = new Explosion(x, y);
            expList.Add(exp);

        }
        public static void CreateBullet(int x, int y, Tag tag, Direction dir)
        {
            Bullet bullet = new Bullet(x, y, 8, dir, tag);
            
                bulletList.Add(bullet);
        }
        public static void DestroyBullet(Bullet bullet)
        {
                bulletList.Remove(bullet);
        }
        public static void KeyDown(KeyEventArgs args)
        {
            myTank.KeyDown(args);
        }
        public static void KeyUp(KeyEventArgs args)
        {
            myTank.KeyUp(args); 
        }
    }
    
}
